package com.hedl.practice.diyJava.array;

import java.util.Arrays;
import java.util.Collections;

/**
 * @author Big Watermelon
 * @description
 * @date 2024/02/13/22:28
 */
public class ArrayReverse {

    /**
     * 反转数组
     * @param a
     * @param length
     */
    static void reverse_1(int a[],int length){
        int[] b = new int[length];
        int j = length;
        for (int i = 0; i < length; i++){
            b[j-1] = a[i];
            j = j - 1;
        }
        //输入反转数组
        System.out.println("反转后数组是: \n");
        for (int k = 0; k < length; k++){
            System.out.println(b[k]);
        }
    }

    /**
     * 创建方法：第一个与最后一个交互，第二个与倒数第二个交换，以此类推
     * @param a
     * @param length
     */
    static void reverse_2(int a[],int length){
        int i,k,t;
        for (i = 0; i < length / 2; i++){
            t = a[i];
            //前后互换
            a[i] = a[length - i -1];
            a[length - i - 1] = t;
        }
        System.out.println("反转后的数组是： \n");
        for (k = 0; k < length; k++){
            System.out.println(a[k]);
        }
    }

    /**
     * 使用 asList() 方法
     * @param a
     */
    static void reverse_3(Integer a[]){
        Collections.reverse(Arrays.asList(a));
        System.out.println(Arrays.asList(a));
    }

    public static void main(String[] args) {
        int [] arr = {10,20,30,40,50};

        Integer [] arr1 = new Integer[arr.length];
        //reverse_1(arr,arr.length);
        //reverse_2(arr,arr.length);
        for (int i = 0; i < arr.length; i++){
            Integer a = (Integer) arr[i];
            arr1[i] = a;
        }
        reverse_3(arr1);
    }
}
